package com.fui.controller;

import com.alibaba.fastjson.JSON;
import com.fui.domain.SmbmsRole;
import com.fui.domain.SmbmsUser;
import com.fui.service.RoleService;
import com.fui.service.UserService;
import com.fui.utils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    private Logger logger= LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    /**
     * 跳转用户登录界面
     *
     * @return
     */
    @GetMapping(value = {"/login"})
    public String login() {

        return "login";
    }

    /**
     * 用户登录
     *
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/login")
    public String login(SmbmsUser user, Model model, HttpSession session) {
        logger.info("输入的用户名信息",user);
        List<SmbmsUser> users = userService.queryUser(user);
        if (users.size() < 1 || !(user.getUserPassword().equals(users.get(0).getUserPassword()))) {
            model.addAttribute("msg", "用户名或密码不正确，请重新输入!");
            return "login";
        }
        logger.info("登录用户是",users);
        session.setAttribute(Constants.USER_SESSION, users.get(0));
        return "redirect:/index";
    }

    /**
     * 查询用户列表
     *
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView queryUser() {

        ModelAndView mv = new ModelAndView();
        List<SmbmsUser> users = userService.queryUser();
        List<SmbmsRole> roles = roleService.queryRole();
        mv.addObject("roles", roles);
        mv.addObject("users", users);
        mv.setViewName("userindex");
        return mv;
    }


    /**
     * 用户退出
     *
     * @param request
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {

        System.out.println(request.getSession().getAttribute(Constants.USER_SESSION));
        request.getSession().invalidate();
        return "redirect:login";
    }

    /**
     * 跳转添加用户界面
     *
     * @return
     */
    @GetMapping("/addUser")
    public String AddUser() {

        return "useradd";
    }

    /**
     * 操作添加用户界面
     *
     * @return
     */
    @PostMapping("/addUser")
    public String addUser(SmbmsUser user) {
        int result = userService.addUser(user);

        return "redirect:/user/index";
    }

    /**
     * 检查用户是否存在
     *
     * @param user
     * @return
     */
    @GetMapping(value = "/ucexist", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String ucexist(SmbmsUser user) {
        System.out.println("user = " + user);
        List<SmbmsUser> users = userService.queryUser(user);
        String str = users.size() > 0 ? "true" : "false";
        System.out.println(str);
        Map<String, Object> map = new HashMap<>();
        map.put("flag", str);
        return JSON.toJSONString(map);
    }

    /**
     * 跳转修改密码界面
     *
     * @return
     */
    @GetMapping("/modifypwd")
    public String modifyPwd() {

        return "usermodifypwd";
    }

    /**
     * 密码修改
     *
     * @return
     */
    @PostMapping("/modifypwd")
    public String modifyPwd(SmbmsUser user) {

        return "usermodifypwd";
    }
}
